package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.PaymentsModel;
import com.carpark.ejb.Model.ReceivablesModel;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Krzysztof
 */
@Stateful
public class PaymentsDAOImpl implements PaymentsDAO {

    private EntityManager entityManager;

    public PaymentsDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public List<PaymentsModel> getAllPayments() {
        return (List<PaymentsModel>) entityManager.createNamedQuery("PaymentsModel.findAll").getResultList();
    }

    @Override
    public int getPaymentsSize() {
        return entityManager.createNamedQuery("PaymentsModel.findAll").getResultList().size();
    }

    @Override
    public PaymentsModel getPaymentsById(Integer id) {
        return (PaymentsModel) entityManager.createNamedQuery("PaymentsModel.findByPaymentId").setParameter("paymentId", id).getSingleResult();
    }

    @Override
    public List<PaymentsModel> getPaymentsByPaymentDate(Date paymentDate) {
        return (List<PaymentsModel>) entityManager.createNamedQuery("PaymentsModel.findByPaymentDate").setParameter("paymentDate", paymentDate).getResultList();
    }

    @Override
    public List<PaymentsModel> getPaymentsByPaymentForm(String paymentForm) {
        return (List<PaymentsModel>) entityManager.createNamedQuery("PaymentsModel.findByPaymentForm").setParameter("paymentForm", paymentForm).getResultList();
    }

    @Override
    public void addPayment(PaymentsModel payment) {
        entityManager.getTransaction().begin();
        entityManager.persist(payment);
        entityManager.flush();
        entityManager.refresh(payment);
        entityManager.persist(payment);
        entityManager.getTransaction().commit();
    }

    @Override
    public void updatePayment(PaymentsModel payment) {
        PaymentsModel rowToUpdate = getPaymentsById(payment.getPaymentId());

        entityManager.getTransaction().begin();
        rowToUpdate.setPaymentDate(payment.getPaymentDate());
        rowToUpdate.setPaymentForm(payment.getPaymentForm());
        rowToUpdate.setReceivableId(payment.getReceivableId());
        entityManager.getTransaction().commit();
    }

    @Override
    public void removePayment(Integer id) {
        PaymentsModel rowToDelete = getPaymentsById(id);

        entityManager.getTransaction().begin();
        entityManager.remove(rowToDelete);
        entityManager.getTransaction().commit();
    }

    @Override
    public int getByPaymentDateSize(Date paymentDate) {
        List<PaymentsModel> list = (List<PaymentsModel>) entityManager.createNamedQuery("PaymentsModel.findByPaymentDate").setParameter("paymentDate", paymentDate).getResultList();
        return list.size();
    }

    @Override
    public List<PaymentsModel> getPaymentsByClient(ClientsModel cl) {
        List<PaymentsModel> list = (List<PaymentsModel>) entityManager.createNamedQuery("PaymentsModel.findByClientId").setParameter("clientId", cl).getResultList();
        return list;
    }

    @Override
    public List<ReceivablesModel> getPayedReceivables() {
        List<ReceivablesModel> list = (List<ReceivablesModel>) entityManager.createNamedQuery("PaymentsModel.findPayedReceivables").getResultList();
        return list;
    }
}